package com.semanticweb.webapp.model.ontology;

import org.apache.lucene.document.Document;

import com.semanticweb.webapp.ontology.CIDOC;
import com.semanticweb.webapp.ontology.Ont;
import com.semanticweb.webapp.ontology.Query;
import com.semanticweb.webapp.search.IDocument;
import com.semanticweb.webapp.search.SearchMD;

/**
 * Class which represents 'Stanje i stepen ocuvanosti' from the chart.
 * 
 * @author Lab
 * 
 */
public class Condition extends DefaultAbstractEntity<String> implements IDocument {

    public static final String ONT_CONDITION = "condition";

    public Condition(final String id) {
        super(id);
    }

    @Override
    public void toRDF() {
        if (this.value == null) {
            return;
        }

        String conditionURI;
        final String itemURI = Item.formatItemURI(this.id);
        final Query query = new Query();
        final String[] find = query.select("e3").type("e3", CIDOC.E3.queryValue()).label("e3", this.value).find();
        if (find != null && find.length > 0) {
            conditionURI = find[0];
        } else {
            conditionURI = Ont.formatURI(this.id, ONT_CONDITION);

            Ont.getOntology().createIndividual(CIDOC.E3.getFullValue(), conditionURI);
            Ont.getOntology().addLabel(conditionURI, this.value);
        }

        Ont.getOntology().connectIndividuals(itemURI, CIDOC.P44.getFullValue(), conditionURI);
    }

    public void fromRDF() {
        final String itemValueURI = Item.formatItemQueryURI(this.id);
        final Query query = new Query();
        final String[] result = query.selectLabel().connect(itemValueURI, CIDOC.P44.queryValue(), "condition")
                .type("condition", CIDOC.E3.queryValue()).findLabel("condition");
        if (result != null && result.length > 0) {
            this.value = result[0];
        }
    }

    @Override
    public void toIndexDocument(final Document doc) {
        if (this.value != null) {
            doc.add(SearchMD.CONTENT.createField(this.value));
        }
    }

}
